<?php

namespace maotool\aop;

use maotool\object\ReflectionUtil;

/**
 * 切面代理
 */
class ProxyClass
{

    protected $instance_5698e30a3e171b662711d3916069e9a;

    protected $callback_5698e30a3e171b662711d3916069e9a;

    /**
     * @param $instance
     * @param ProxyClassCallback $callback
     */
    public function __construct($instance, $callback)
    {
        $this->instance_5698e30a3e171b662711d3916069e9a = $instance;
        $this->callback_5698e30a3e171b662711d3916069e9a = $callback;
    }

    /**
     * @param string $name
     * @param array $arguments
     * @return mixed
     */
    public function __call($name, $arguments)
    {
        $param = $this->callback_5698e30a3e171b662711d3916069e9a
            ->callBefore($this->instance_5698e30a3e171b662711d3916069e9a, $name, $arguments);
        $result = ReflectionUtil::method([$this->instance_5698e30a3e171b662711d3916069e9a, $name], $param);
        return $this->callback_5698e30a3e171b662711d3916069e9a
            ->callAfter($this->instance_5698e30a3e171b662711d3916069e9a, $name, $result);
    }

    /**
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        return $this->callback_5698e30a3e171b662711d3916069e9a->get(
            $name,
            ReflectionUtil::getProperty($this->instance_5698e30a3e171b662711d3916069e9a, $name)
        );
    }

    /**
     * @param string $name
     * @param $value
     * @return void
     */
    public function __set($name, $value)
    {
        ReflectionUtil::property($this->instance_5698e30a3e171b662711d3916069e9a,
            $name,
            $this->callback_5698e30a3e171b662711d3916069e9a->set($name, $value));
    }

}